package com.gress.shop.dao.impl;

import com.google.common.collect.Lists;
import com.gress.shop.bo.MemberRoleRelation;
import com.gress.shop.bo.example.MemberRoleRelationExample;
import com.gress.shop.dao.IMemberRoleRelationDao;
import com.gress.shop.mapper.MemberRoleRelationMapper;
import com.gress.shop.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created xiongwen with 2018/4/12 10:16
 * Description:
 */
@Service
public class MemberRoleRelationDaoImpl implements IMemberRoleRelationDao {

    @Resource
    private MemberRoleRelationMapper memberRoleRelationMapper;

    @Override
    public int countByExample(MemberRoleRelationExample example) {
        return memberRoleRelationMapper.countByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return memberRoleRelationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(MemberRoleRelation record) {
        String uuid = UUIDUtil.getUUID();
        record.setId(uuid);
        return memberRoleRelationMapper.insert(record);
    }

    @Override
    public int insertSelective(MemberRoleRelation record) {
        String uuid = UUIDUtil.getUUID();
        record.setId(uuid);
        return memberRoleRelationMapper.insertSelective(record);
    }

    @Override
    public List<MemberRoleRelation> selectByExample(MemberRoleRelationExample example) {
        return memberRoleRelationMapper.selectByExample(example);
    }

    @Override
    public List<MemberRoleRelation> selectAll() {
        MemberRoleRelationExample example = new MemberRoleRelationExample();
        example.createCriteria().andIsValidEqualTo(1);
        return memberRoleRelationMapper.selectByExample(example);
    }

    @Override
    public MemberRoleRelation selectByPrimaryKey(String id) {
        return memberRoleRelationMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(MemberRoleRelation record) {
        return memberRoleRelationMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(MemberRoleRelation record) {
        return memberRoleRelationMapper.updateByPrimaryKey(record);
    }

    @Override
    public MemberRoleRelation selectByMemberId(String email) {
        MemberRoleRelationExample example = new MemberRoleRelationExample();
        example.createCriteria().
                andMemberIdEqualTo(email).
                andIsValidEqualTo(1);
        List<MemberRoleRelation> memberRoleRelations = memberRoleRelationMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(memberRoleRelations)) {
            return null;
        }
        return memberRoleRelations.get(0);
    }

    @Override
    public List<MemberRoleRelation> selectByRoleId(String roleId) {
        MemberRoleRelationExample example = new MemberRoleRelationExample();
        example.createCriteria().
                andRoleIdEqualTo(roleId).
                andIsValidEqualTo(1);
        List<MemberRoleRelation> memberRoleRelations = memberRoleRelationMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(memberRoleRelations)) {
            return Lists.newArrayList();
        }
        return memberRoleRelations;
    }
}
